package website.lucaswork.survey.core.entities;

import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * The persistent class for the su_user database table.
 */
@Entity
@Table(name = "su_user")
public class User implements Serializable, UserDetails {
    private static final long serialVersionUID = 1L;

    public enum UserStatus {
        STATUS_INACTIVE, STATUS_ACTIVE;
    }
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "auth_key")
    private String authKey;

    @Column(name = "created_at", insertable = false, updatable = false)
    private Date createdAt;

    private String email;

    @Column(name = "password_hash")
    private String passwordHash;

    @Column(name = "password_reset_token")
    private String passwordResetToken;

    @Enumerated(EnumType.ORDINAL)
    private UserStatus status;

    @Column(name = "updated_at", insertable = false, updatable = false)
    private Timestamp updatedAt;

    private String username;

    //bi-directional many-to-one association to Survey
    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    private List<Survey> surveys;

    //bi-directional many-to-one association to Authority
    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    private List<Authority> authorities;

    public User() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAuthKey() {
        return this.authKey;
    }

    public void setAuthKey(String authKey) {
        this.authKey = authKey;
    }

    public Date getCreatedAt() {
        return this.createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPasswordHash() {
        return this.passwordHash;
    }

    public void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    public String getPasswordResetToken() {
        return this.passwordResetToken;
    }

    public void setPasswordResetToken(String passwordResetToken) {
        this.passwordResetToken = passwordResetToken;
    }

    public UserStatus getStatus() {
        return this.status;
    }

    public void setStatus(UserStatus status) {
        this.status = status;
    }

    public Timestamp getUpdatedAt() {
        return this.updatedAt;
    }

    public void setUpdatedAt(Timestamp updatedAt) {
        this.updatedAt = updatedAt;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public List<Survey> getsurveys() {
        return this.surveys;
    }

    public void setSurveys(List<Survey> surveys) {
        this.surveys = surveys;
    }

    public Survey addSurvey(Survey survey) {
        getsurveys().add(survey);
        survey.setUser(this);

        return survey;
    }

    public Survey removeSurvey(Survey survey) {
        getsurveys().remove(survey);
        survey.setUser(null);

        return survey;
    }

    public List<Authority> getAuthorities() {
        if (this.authorities == null) {
            this.authorities = new ArrayList<Authority>();
        }
        return this.authorities;
    }

    public void setAuthorities(List<Authority> authorities) {
        this.authorities = authorities;
    }

    public Authority addAuthority(Authority authority) {
        getAuthorities().add(authority);
        authority.setUser(this);

        return authority;
    }

    public Authority removeAuthority(Authority authority) {
        getAuthorities().remove(authority);
        authority.setUser(null);

        return authority;
    }

    @Override
    public String getPassword() {
        return this.passwordHash;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.status == UserStatus.STATUS_ACTIVE;
    }

}